package cn.com.zhbs.account.service.impl;

import cn.com.zhbs.account.dao.DepartmentMapper;
import cn.com.zhbs.account.dao.RoleMapper;
import cn.com.zhbs.account.entitys.Department;
import cn.com.zhbs.account.entitys.Menu;
import cn.com.zhbs.account.entitys.Node;
import cn.com.zhbs.account.entitys.Role;
import cn.com.zhbs.account.service.RoleService;
import cn.com.zhbs.account.util.TreeRecursion;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: vr_manage
 * @description: 角色实现层
 * @author: 陈治杰
 * @create: 2021-08-07 18:52
 **/
@Service
public class RoleServiceImpl implements RoleService {
    @Resource
    RoleMapper roleMapper;
    @Resource
    DepartmentMapper departmentMapper;
    @Override
    public Map<String, Object> roleList(JSONObject jsonObject) {
        Map<String,Object> result=new HashMap<>();
        Integer currentPage=Integer.parseInt(jsonObject.getString("currentPage"));
        Integer pageSize=Integer.parseInt(jsonObject.getString("pageSize"));
        Integer start=(currentPage-1)*pageSize;
        jsonObject.put("start",start);
        jsonObject.put("pageSize",pageSize);
        jsonObject.put("dids",departmentMapper.findDIDByAID(jsonObject));
        result.put("total",roleMapper.roleTotal(jsonObject));
        result.put("list", roleMapper.findRolesByDID(jsonObject));
        return result;
    }

    @Override
    public void addRole(JSONObject jsonObject) {
        roleMapper.insertRole(jsonObject);
        //获取角色菜单
        List<Integer> resources = (List<Integer>) jsonObject.get("resource_id");
        List<Integer> dids=roleMapper.findDIDByAID(jsonObject);
        for(Integer did:dids){
            JSONObject json=new JSONObject();
            json.put(Department.did.toString(),did);
            json.put(Role.role_id.toString(),jsonObject.get(Role.role_id.toString()));
            roleMapper.insertRoleDepartment(json);
        }
        for(Integer resource:resources){
            JSONObject json=new JSONObject();
            json.put(Role.role_id.toString(),jsonObject.get(Role.role_id.toString()));
            json.put(Menu.resource_id.toString(),resource);
            roleMapper.insertRoleResource(json);
        }
    }

    @Override
    public void editRole(JSONObject jsonObject) {
        roleMapper.updateRole(jsonObject);

        //获取角色菜单
        roleMapper.deleteResource(jsonObject);
        List<Integer> resources = (List<Integer>) jsonObject.get("resource_id");
        for(Integer resource:resources){
            JSONObject json=new JSONObject();
            json.put(Role.role_id.toString(),jsonObject.get(Role.role_id.toString()));
            json.put(Menu.resource_id.name(),resource);
            roleMapper.insertRoleResource(json);
        }
    }

    @Override
    public Map<String, Object> roleDetail(JSONObject jsonObject) {
        Map<String,Object> role=roleMapper.findRoleByRoleID(jsonObject);
        List<Map<String,String>> dataList=roleMapper.findResourceByRoleID(jsonObject);
        Map<String, Node> nodes = new HashedMap();
        for(int i=0;i<dataList.size();i++){
            //模拟数据库存储树结构
            nodes.put(""+i+1,new Gson().fromJson(dataList.get(i).toString(),Node.class));
        }
        role.put("menu", TreeRecursion.sortList(TreeRecursion.getNodeJson("0",nodes)));
        return role;
    }

    @Override
    public void deleteRole(JSONObject jsonObject) {
            roleMapper.deleteRole(jsonObject);
    }
}
